<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="/css/shangjia-ued.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/js/lib/jquery.js"></script>
<script type="text/javascript" src="/js/plugin/form/jquery.form.js"></script>
<script type="text/javascript" src="/js/plugin/validate/jquery.validate.js"></script>
</head>
<body>
<div style="padding:40px;">
    <label class="validError"><b>大家大大大大大大大大好</b></label>
    <label class="validRight"><b>大家大大大大大大大大好</b></label>
    <label class="validClue"><b>大家大大大大大大大大好</b></label>
</div>


<form id="myFormCompanyDetail" method="post">
<div>
<textarea id="validateResult" style="width:400px;height:50px;margin-bottom:10px;">
{corporationf:"长度小于20"}
</textarea>
</div>
  <table>
    <tr>
      <td>法人代表:</td>
      <td><input type="text" name="corporation" /></td>
    </tr>
    <tr>
      <td>法人代2表:</td>
      <td><input type="text" name="corporationf" /></td>
    </tr>
    <tr>
      <td>城市联动:</td>
      <td><select name="province"><option selected="selected" value="">请选择</option><option value="北京">北京</option></select><select name="city"><option selected="selected" value="">请选择</option><option value="朝阳区">朝阳区</option></select><input type="text" name="address"/></td>
    </tr>
  </table>
  <div><input id="save" type="submit" value="保存"/></div>
</form>
<script type="text/javascript">


/**
* @hz
* @title 服务端表单错误信息返回处理
* @param form 表单jquery对象
* @demo severErrorInset("#form")
*/
function severErrorInset(form){
	var data = $.trim(form.find("#validateResult").val());
	if(data == ""){return false};
	data = eval("("+data+")");
	for(var i in data){
		data[i] = "<b>"+data[i]+"</b>";
	}
	form.validate().showErrors(data)
} 


/**
* @hz
* @title 表单验证错误信息位置
* @param error Object 错误信息对象
* @param element Object 信息对象
* @demo errorPlacement
*/
function errorPlacement(error, element){
	var par = element.parent();
	par.find("label.validError").remove(); 
	par.append(error);
}

var form = $("#myFormCompanyDetail")

form.validate({
	errorPlacement:errorPlacement, //设置错误信息位置
	errorClass:"validError",//设置错误的样式
	debag:true,
	rules:{
		corporation:{
			required:true,
			maxlength:22,
			minlength:4
		},
		address:{
			cityLinkage:'cityLinkage'
		}
	},
	messages:{
		corporation:{
			required:"<b>法人必填</b>",
			maxlength:"<b>最多22个字符</b>",
			minlength:"<b>最小4个字符</b>"
		}
	}

})

severErrorInset(form);//插入服务端错误

// 城市联动验证   
jQuery.validator.addMethod("cityLinkage", function(value, element, param) {   
	var province = form.find("[name='province']").val() ||  false;
	var city = form.find("[name='city']").val() || false;
	var address = form.find("[name='address']").val() || false ;
	return province && city && address && true;
}, "<b>亲～ 城市必填哦～</b>"); 



</script>
</body>
</html>
